<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<title>APP</title>
		<link rel="stylesheet" type="text/css" href="../css/aui.css" />
	</head>
	<style type="text/css">
		.aui-list-view-cell {
			line-height: 22px;
		}
		#discover .category {
			position: absolute;
			background-color: #fff;
			width: 100%;
			border-bottom: 1px solid #dedede;
			height: 44px;
			z-index: 20;
			overflow: hidden;
			opacity: 0;
			display: none;
			-webkit-transition: all .3s;
			transition: all .3s;
		}
		.aui-input-addon {
			font-size: 1.0em;
		}
		.aui-radio {
			width: 26px;
			height: 26px;
		}
		.aui-radio-name {
			font-size: 0.9em;
		}
		.pos-buttom-50 {
			margin-top: 30%;
		}
		.pos-top-40 {
			margin-top: 40px;
		}
	</style>
	<body>
		<p class="aui-padded-10">
			如果您是本人签名，可以选择一个自己的用户进行真实签名书写和模仿别人的用户签名书写。
		</p>
		<div class="aui-content  aui-card">
			<div class="aui-form">
				<!--<div class="aui-input-row">
				<span class="aui-input-addon">是否本人：</span>
				<input type="text" class="aui-input" placeholder="请输入签名用户"/>
				</div>-->
				<div class="aui-input-row">
					<span class="aui-input-addon">是否本人：</span>
					<div class="aui-pull-right">
						<input class="aui-radio sigtype" type="radio" name="demo1" checked onclick="SetUserType(0)" tapmode="">
						<div class="aui-radio-name">
							真实签名
						</div>
						<input class="aui-radio sigtype" type="radio" name="demo1"  onclick="SetUserType(1)" tapmode="">
						<div class="aui-radio-name">
							伪造签名
						</div>
					</div>
				</div>
				<div class="aui-input-row" id="discover">
					<span class="aui-input-addon">用户姓名:</span>
					<input type="text" class="aui-input" id="signame" placeholder="请输入签名用户的姓名"/>
					<span class="aui-input-addon">
						<div class="aui-btn" id="expandMore" tapmode onclick="expandMore()">
							签名用户
						</div> </span>
				</div>
			</div>
		</div>
		<div class="aui-content aui-card pos-buttom-501 pos-top-40">
			<div class="aui-btn aui-btn-block aui-btn-success" onclick="SaveUserName()">
				确&nbsp&nbsp&nbsp认
			</div>
		</div>
		<div class="aui-content aui-card pos-buttom-501 pos-top-40">
			<ul class="aui-list-view aui-grid-view">
				<li class="aui-list-view-cell aui-img aui-col-xs-6">
					<div class="aui-img-body">
						安卓
					</div>
					<img id="AndroidUrl" class="aui-img-object" src="">
				</li>
				<li class="aui-list-view-cell aui-img aui-col-xs-6">
					<div class="aui-img-body">
						IOS
					</div>
					<img id="IOSUrl" class="aui-img-object" src="">
				</li>
			</ul>
		</div>
	</body>
	<script type="text/javascript" src="../script/api.js" ></script>
	<script type="text/javascript" src="../script/zepto.js" ></script>
	<script type="text/javascript" src="../script/APICloud-rest-SHA1.js"></script>
	<script type="text/javascript">
		var APPID = "A6996268375072";
		var APPKEY = "DF2513D5-2329-E8B0-7064-A9D8D701D282";
		//显示本机所有用户
		var signame = "";
		var sigtype = "_T_";
		function expandMore() {
			var h = api.winHeight;
			var h1 = parseInt(h * 0.25);
			var h3 = 60;
			var h2 = h - h1 - h3;
			//console.log(h+","+h1+","+h2+","+h3);
			setTimeout(function() {
				api.openFrame({
					name : 'userlist_frm3',
					url : 'userlist_frm3.html',
					bounces : false,
					bgColor : 'rgba(0,0,0,0.6)',
					vScrollBarEnabled : false,
					hScrollBarEnabled : false,
					rect : {
						x : 0,
						y : h1 + h2,
						w : 'auto',
						h : h3
					}
				});
			}, 150);
			setTimeout(function() {
				api.openFrame({
					name : 'userlist_frm2',
					url : 'userlist_frm2.html',
					bounces : false,
					bgColor : 'rgba(0,0,0,0.6)',
					vScrollBarEnabled : false,
					hScrollBarEnabled : false,
					rect : {
						x : 0,
						y : h1,
						w : 'auto',
						h : h2
					}
				});
			}, 100);
			setTimeout(function() {
				api.openFrame({
					name : 'userlist_frm1',
					url : 'userlist_frm1.html',
					bounces : true,
					bgColor : 'rgba(0,0,0,0.6)',
					vScrollBarEnabled : false,
					hScrollBarEnabled : false,
					rect : {
						x : 0,
						y : 0,
						w : 'auto',
						h : h1
					}
				});
			}, 100);
		}

		var userlist = [];
		function LoadUserList() {
			api.readFile({
				path : 'fs://userlist.json',
			}, function(ret, err) {
				if (ret.status) {
					userlist = JSON.parse(ret.data);
				}
			});
		}

		function SaveUserList() {
			api.writeFile({
				path : 'fs://userlist.json',
				data : JSON.stringify(userlist)
			}, function(ret, err) {
				//alert(JSON.stringify(ret));
				//alert(JSON.stringify(err));
			});
		}

		//设置用户名
		function SetUserName() {
			if (signame) {
				var el = $api.byId('signame');
				$api.val(el, signame);
			}
		}

		//设置用户类型
		function SetUserType(index) {
			if (index == 0)
				sigtype = "_T_";
			else if (index == 1)
				sigtype = "_F_";
			else
				sigtype = "_V_";
		}


		//得到用户类型
		function SetUserType(index) {
			if (index == 0)
				sigtype = "_T_";
			else if (index == 1)
				sigtype = "_F_";
			else
				sigtype = "_V_";
		}

		function LoadUserType(type) {
			var $types = $api.domAll('.sigtype');
			if (type == "_T_")
				$types[0].checked = true;
			else
				$types[1].checked = true;
		}

		function SaveUserName() {
			var el = $api.byId('signame');
			signame = $api.val(el);
			var i = 0;
			for (; i < userlist.length; i++)
				if (userlist[i] == signame)
					break;
			if (i == userlist.length) {
				userlist.push(signame);
				userlist.sort();
				SaveUserList();
			}
			$api.setStorage("signame", signame);
			$api.setStorage("sigtype", sigtype);
			api.sendEvent({
				name : 'ChangeSigName',
				extra : {
					signame : signame,
					sigtype : sigtype
				}
			});
			api.closeWin();
		}

		apiready = function() {
			signame = $api.getStorage("signame") || "测试名";
			sigtype = $api.getStorage("sigtype") || "_T_";
			LoadUserType(sigtype);
			SetUserName();
			LoadUserList();
			api.addEventListener({
				name : 'ChangeUser'
			}, function(ret, err) {
				signame = ret.value;
				SetUserName();
			});
			api.addEventListener({
				name : 'viewappear'
			}, function(ret, err) {
				LoadUserType(sigtype);
				SetUserName();
				LoadUserList();
			});
			var client = new Resource(APPID, APPKEY);
			var model = client.Factory("Setting");
			model.query({
				"filter" : {
					"where" : {},
					"skip" : 0,
					"limit" : 20
				}
			}, function(ret, err) {				
				console.log(JSON.stringify(ret));
				if(ret&&ret.length>0)
				{					
					for(var i=0;i<ret.length;i++)
					{
						var data=ret[i]||{};
						if(data.name=="AndroidUrl"&&data.value)
						{
							var el=$api.byId('AndroidUrl');
							$api.attr(el, 'src', data.value);
						}
						if(data.name=="IOSUrl"&&data.value)
						{
							var el=$api.byId('IOSUrl');
							$api.attr(el, 'src', data.value);
						}
					}				
				}
			});
		}
	</script>
</html>